package queuedemo;


public class MyLinkQueue {

    public static class ListNode{
        public int val;
        ListNode next;
        ListNode prev;

        public ListNode(int val) {
            this.val = val;
        }
    }

    private int usedSize;
    public ListNode head;
    public ListNode last;

    public boolean offer(int val) {
        ListNode node = new ListNode(val);
        if(head==null) {
            head = node;
            last = node;
        }else {
            last.next = node;
            node.prev = last;
            last = last.next;
        }
        usedSize++;
        return true;
    }

    public int poll() {
        if(head == null) {
            return -1;
        }
        usedSize--;
        int retVal = head.val;

        //只有一个节点的情况
        if(head.next == null){

            head = null;
            last = null;
            return retVal;
        }

        head = head.next;
        head.prev = null;
        return retVal;
    }

    public int peek() {
        if(head==null) {
            return -1;
        }
        return head.val;
    }

    public int size() {
        return usedSize;
    }

    boolean isEmpty(){
        return head==null;
    }
}
